<?php
Zend_Loader::loadClass('Lead_Db_Adapter_Interface');
Zend_Loader::loadClass('Zend_Db_Adapter_Pdo_Pgsql');

/**
 * Lead_Db PostgreSQL adapter.
 * 
 * @category Lead
 * @package Db 
 *
 */
class Lead_Db_Adapter_Pdo_Pgsql extends Zend_Db_Adapter_Pdo_Pgsql implements Lead_Db_Adapter_Interface
{
    
    private static $nativeToCommonMap = array
	(
		'int2' => array('type' => 'smallint', 'options' => array()),	
		'int4' => array('type' => 'integer', 'options' => array()),
		'int8' => array('type' => 'bigint', 'options' => array()),
		'varchar' => array('type' => 'string', 'options' => array()),
		'bpchar' => array('type' => 'char', 'options' => array()),
		'text' => array('type' => 'text', 'options' => array()),
		'timestamp' => array('type' => 'datetime', 'options' => array()),
		'date' => array('type' => 'date', 'options' => array()),
		'float4' => array('type' => 'single', 'options' => array()),
		'float8' => array('type' => 'double', 'options' => array()),
		'bool' => array('type' => 'boolean', 'options' => array()),
		'numeric' => array('type' => 'decimal', 'options' => array()),
	);

	private static $commonToNativeMap = array(
		'smallint' => array('type' => 'smallint', 'options' => array()),	
		'integer' => array('type' => 'integer', 'options' => array()),
		'bigint' => array('type' => 'bigint', 'options' => array()),
		'string' => array('type' => 'character varying', 'options' => array('size' => 255)),
		'char' => array('type' => 'character', 'options' => array()),
		'text' => array('type' => 'text', 'options' => array()),
		'datetime' => array('type' => 'timestamp', 'options' => array()),
		'date' => array('type' => 'date', 'options' => array()),
		'single' => array('type' => 'real', 'options' => array()),
		'double' => array('type' => 'double precision', 'options' => array()),
		'boolean' => array('type' => 'boolean', 'options' => array()),
		'decimal' => array('type' => 'numeric', 'options' => array()),
	);
	
	
	public static function nativeToCommon($columns)
	{
	    foreach($columns as &$column)
	    {
			if(key_exists($column['DATA_TYPE'], self::$nativeToCommonMap))
                $column['DATA_TYPE'] = self::$nativeToCommonMap[$column['DATA_TYPE']]['type'];
	    }
	    return $columns;
	}
       
    
    
    public function nextId($sequenceName)
    {
    	do
    	{
    		$count = 0;
    	    
    	    $phrase = "SELECT NEXTVAL('{$sequenceName}')";
			
    	    try
    	    {
    	        $nextid = $this->fetchOne($phrase);
    	    }
    	    catch(Zend_Db_Statement_Exception $e)
    	    {
    	        $this->createSequence($sequenceName);
    	    }
    	    
			$count ++;
			
			if($count > 10)
        		 throw new Exception('PoopingLooper started in LDB_pgsql');
			
    	} while(!$nextid);
		
    	return $nextid;
    }

	
    public function createSequence($sequenceName)
	{
	    $phrase = "CREATE SEQUENCE {$sequenceName}";
	    $this->query($phrase);
	}

	
	public function formatFieldForSQL(Lead_EDO_Datatype_Field_Value $field)
	{
	    if($field->getType() == 'Boolean') {
	        return ($field->getValue() === true) ? 'T' : 'F';
	    }
	    
	    return $field->getValue();
	    
	}
    
	
    
    
}